//
//  WmcPresentVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/3.
//

import SnapKit
import UIKit
import YPBedrock

public extension WmcPresentVC {
    struct Config {
        public struct HollowRect {
            /// 区域
            public var rect: CGRect
            /// 边框圆角位置
            public var roundingCorners: UIRectCorner = .allCorners
            /// 圆角大小
            public var cornerRadii: CGFloat = 0
            /// 是否镂空处理
            public var isHollow: Bool = true
            /// 事件是否透传
            public var isHitPassthrough: Bool = false
            /// 透传后是否自动消失
            public var isHitPassthroughAutoDismiss: Bool = true
        }

        // Public

        /// 样式
        public var style: WmcPresentVC.Style = .alert()

        /// 内容视图
        public var contentView: UIView = .init()

        /// 视图布局回调
        public var contentLayoutBlock: YPBlock.OneParam<ConstraintMaker>? = nil

        /// 是否允许蒙版点击dismiss
        public var isTouchMaskDismiss: Bool = true

        /// 点击蒙版回调
        public var touchMaskBlock: YPBlock.Empty? = nil

        /// 背景色
        public var backgroundColor: UIColor = .black.yp.alpha(0.3)

        /// 镂空区域
        public var hollowRects: [HollowRect] = []

        /// 基础配置
        public static func basicConfig(view: UIView,
                                       style: Style = .alert(),
                                       layoutBlock: YPBlock.OneParam<ConstraintMaker>? = nil) -> Config
        {
            var config: Config = .init()
            config.contentView = view
            config.style = style
            config.contentLayoutBlock = layoutBlock
            return config
        }

        // Internal

        /// 默认配置
        static var defaultConfig: Config {
            self.init()
        }
    }
}

// MARK: - [WmcPresentVC]

open class WmcPresentVC: WmcBaseVC {
    // Lifecycle

    init(config: Config = .defaultConfig) {
        super.init(nibName: nil, bundle: nil)
        self.config = config
    }

    @available(*, unavailable)
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override open func viewDidLoad() {
        super.viewDidLoad()
        transitioningDelegate = self // 配置代理
    }

    // Open

    /// 展示出来
    open func show(_ completion: YPBlock.Empty? = nil) {
        YPApp.CurrentVC?.present(self, animated: true, completion: completion)
    }

    // Public

    /// present样式
    override public var modalPresentationStyle: UIModalPresentationStyle {
        set { super.modalPresentationStyle = newValue }
        get { return .overFullScreen }
    }

    // Internal

    /// 配置对象
    var config: Config = .defaultConfig

    /// 背景视图
    lazy var backgroundControl: WmcPresentBackControl = {
        let control = WmcPresentBackControl()
        control.hollowRects = config.hollowRects
        control.backgroundColor = config.backgroundColor
        control.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.config.touchMaskBlock?()
            if weakSelf.config.isTouchMaskDismiss {
                weakSelf.willTouchBackgroundDismissCompletionBlock?()
                weakSelf.dismiss(animated: true, completion: { [weak self] in
                    guard let weakSelf = self else { return }
                    weakSelf.didTouchBackgroundDismissCompletionBlock?()
                })
            }
        }
        control.hitPassthroughBlock = { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.view.superview?.isUserInteractionEnabled = false
            weakSelf.yp.delayLast(0.5) {
                weakSelf.view.superview?.isUserInteractionEnabled = true
            }
        }
        control.hitTestDismissBlock = { [weak self] _ in
            guard let weakSelf = self else { return }
            if weakSelf.config.isTouchMaskDismiss {
                YPGCD.Delay(delay: 0.1) {
                    weakSelf.willTouchBackgroundDismissCompletionBlock?()
                    weakSelf.dismiss(animated: true, completion: { [weak self] in
                        guard let weakSelf = self else { return }
                        weakSelf.didTouchBackgroundDismissCompletionBlock?()
                    })
                }
            }
        }
        return control
    }()

    /// 动画管理器
    lazy var presentAnimation: WmcPresentAnimation = {
        let animation = WmcPresentAnimation()
        return animation
    }()

    var willTouchBackgroundDismissCompletionBlock: YPBlock.Empty?
    var didTouchBackgroundDismissCompletionBlock: YPBlock.Empty?

    override func makeUI() {
        view = backgroundControl
    }
}

// MARK: - UIViewControllerTransitioningDelegate

extension WmcPresentVC: UIViewControllerTransitioningDelegate {
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentAnimation.state = .present
        return presentAnimation
    }

    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentAnimation.state = .dismiss
        return presentAnimation
    }
}
